﻿/*CLR Version: 4.0.30319.18408
 * Creat Date: 2015/6/18 16:08:36
 * Creat Year: 2015
 * Creator: 程炜.Snail
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;

namespace Utility.Extensions
{
    /// <summary>
    /// 指示在调用 StrConv 函数时要执行哪种类型的转换。
    /// </summary>
    public enum VbStrConv
    {
        /// <summary>
        /// 不执行转换。
        /// </summary>
        None = 0,
        /// <summary>
        /// 将字符串转换为大写字符。该成员等效于 Visual Basic 常数 vbUpperCase。
        /// </summary>
        Uppercase = 1,
        /// <summary>
        /// 将字符串转换为小写字符。该成员等效于 Visual Basic 常数 vbLowerCase。
        /// </summary>
        Lowercase = 2,
        /// <summary>
        /// 将字符串中每个单词的首字母转换为大写。该成员等效于 Visual Basic 常数 vbProperCase。
        /// </summary>   
        ProperCase = 3,
        /// <summary>
        /// 将字符串中的窄（单字节）字符转换为宽（双字节）字符。应用于亚洲区域设置。该成员等效于 Visual Basic 常数 vbWide。
        /// </summary>
        Wide = 4,
        /// <summary>
        /// 将字符串中的宽（双字节）字符转换为窄（单字节）字符。应用于亚洲区域设置。该成员等效于 Visual Basic 常数 vbNarrow。
        /// </summary>
        Narrow = 8,
        /// <summary>
        /// 将字符串中的平假名字符转换为片假名字符。仅应用于日文区域设置。该成员等效于 Visual Basic 常数 vbKatakana。
        /// </summary>
        Katakana = 16,
        /// <summary>
        /// 将字符串中的片假名字符转换为平假名字符。仅应用于日文区域设置。该成员等效于 Visual Basic 常数 vbHiragana。
        /// </summary>
        Hiragana = 32,
        /// <summary>
        /// 将字符串转换为简体中文字符。该成员等效于 Visual Basic 常数 vbSimplifiedChinese。
        /// </summary>
        SimplifiedChinese = 256,
        /// <summary>
        /// 将字符串转换为繁体中文字符。该成员等效于 Visual Basic 常数 vbTraditionalChinese。
        /// </summary>    
        TraditionalChinese = 512,
        /// <summary>
        /// 将字符串从大小写文件系统规则转换为语义规则。该成员等效于 Visual Basic 常数 vbLinguisticCasing。
        /// </summary>    
        LinguisticCasing = 1024,
    }
    /// <summary>
    /// 字符串扩展方法
    /// </summary>
    public static class StringExtensions
    {
        /// <summary>
        /// 语言转换(简/繁转换)
        /// </summary>
        /// <param name="str"></param>
        /// <param name="strConv">需要转换的文字</param>
        /// <returns>返回转换后的文字</returns>
        public static string StrConv(this string str, VbStrConv strConv)
        {
            if (string.IsNullOrEmpty(str))
            {
                return str;
            }
            return Microsoft.VisualBasic.Strings.StrConv(str, (Microsoft.VisualBasic.VbStrConv)strConv, 0);
        }
        /// <summary>
        /// 字符串隐匿
        /// </summary>
        /// <param name="str"></param>
        /// <param name="begin">开始需要转换的位置,从零开始</param>
        /// <param name="end">结束位置</param>
        /// <param name="dormatChar">替换的符号</param>
        /// <returns></returns>
        public static string DormantChar(this string str, int begin, int end, char dormatChar = '*')
        {
            if (string.IsNullOrEmpty(str))
            {
                return str;
            }
            if (begin < 0)
            {
                begin = 0;
            }
            if (begin > str.Length)
            {
                return str;
            }
            if (begin > end)
            {
                return str;
            }
            if (end > str.Length)
            {
                end = str.Length;
            }
            char[] cs = str.ToCharArray();
            for (int i = begin; i < end; i++)
            {
                cs[i] = dormatChar;
            }
            return new string(cs);
        }
        /// <summary>
        /// 字符串隐匿
        /// </summary>
        /// <param name="str"></param>
        /// <param name="retentionCharLength">最后保留的字符数</param>
        /// <param name="dormantChar">替换的符号</param>
        /// <returns></returns>
        public static string DormantChar(this string str, int retentionCharLength, char dormantChar = '*')
        {
            if (string.IsNullOrEmpty(str))
            {
                return str;
            }
            if (retentionCharLength > str.Length)
            {
                retentionCharLength = str.Length;
            }
            char[] cs = str.ToCharArray();
            for (int i = 0; i < (str.Length - retentionCharLength); i++)
            {
                cs[i] = dormantChar;
            }
            return new string(cs);
        }
        /// <summary>
        /// 使用指定占位符格式化字符串,使其长度固定
        /// </summary>
        /// <param name="str"></param>
        /// <param name="stringLength">字符串长度</param>
        /// <param name="placeholder">占位符,默认空格</param>
        /// <param name="isFront">在前面添加</param>
        /// <returns></returns>
        public static string FormatString(this string str, int stringLength, char placeholder =' ',bool isFront=false)
        {
            int strLength = str.Length;
            for (int i = str.Length; i < stringLength; i++)
            {
                if (isFront)
                {
                    str = placeholder + str;
                }
                else
                {
                    str = str + placeholder;
                }
            }
            return str;
        }
        /// <summary>
        /// 字符串,去除空格 有判断是否为空
        /// </summary>
        /// <param name="str"></param>
        /// <param name="trims"></param>
        /// <returns></returns>
        public static string StrTrim(this string str, params char[] trims)
        {
            if (!string.IsNullOrEmpty(str))
            {
                str = str.Trim(trims);
            }
            return str;
        }
        /// <summary>
        /// 将字符串进行base64转换
        /// </summary>
        /// <param name="str">需要转换的字符串明文</param>
        /// <param name="encodingStr">转换使用的Encoding,默认UTF-8</param>
        /// <returns></returns>
        public static string ToBase64String(this string str,string encodingStr ="utf-8")
        { 
            Encoding encoding = Encoding.GetEncoding(encodingStr);
            byte[] strBytes = encoding.GetBytes(str);
            return Convert.ToBase64String(strBytes);
        }
        /// <summary>
        /// 将base64转换成字符串
        /// </summary>
        /// <param name="str">需要转换的字符串密文</param>
        /// <param name="encodingStr">转换使用的Encoding,默认UTF-8</param>
        /// <returns></returns>
        public static string FromBase64String(this string str, string encodingStr = "utf-8")
        {
            byte[] strBytes = Convert.FromBase64String(str);
            Encoding encoding = Encoding.GetEncoding(encodingStr);
            return encoding.GetString(strBytes);
        }
        /// <summary>
        /// 转换成Decimal类型
        /// </summary>
        /// <param name="str"></param>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        public static decimal ToDecimal(this string str, decimal defaultValue = decimal.MinValue)
        {
            if (string.IsNullOrEmpty(str))
            {
                return defaultValue;
            }
            else
            {
                return Convert.ToDecimal(str);
            }
        }
        /// <summary>
        /// 转换成double类型
        /// </summary>
        /// <param name="str"></param>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        public static double ToDouble(this string str, double defaultValue = double.MinValue)
        {
            if (string.IsNullOrEmpty(str))
            {
                return defaultValue;
            }
            else
            {
                return Convert.ToDouble(str);
            }
        }
        /// <summary>
        /// 转换成Int32类型
        /// </summary>
        /// <param name="str"></param>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        public static Int32 ToInt32(this string str, Int32 defaultValue = Int32.MinValue)
        {
            if (string.IsNullOrEmpty(str))
            {
                return defaultValue;
            }
            else
            {
                return Convert.ToInt32(str);
            }
        }
    }
}
